## ANES 2024 Data 
## July 2025 
## Advances Paper   

## Load data and libraries 
library(tidyverse)
library(haven) 
library(pollster)
library(modelsummary)
library(estimatr) 
library(ggpubr)
library(here)
rm(list=ls())

here::i_am("dot_code_figs/fig-3.R")

nes <- read_dta(file = "ANES 2024 Data/anes_timeseries_2024_stata_20250430.dta")
nes <- zap_labels(nes) 


## Test #1: Look at Differences across modes  
nes <- nes %>% 
  mutate(dem_ft = ifelse(V241166 > -1,V241166,NA),
         rep_ft = ifelse(V241167 > -1,V241167,NA),
         ## party ID & ideology 
         party_id = ifelse(V241227x > 0, V241227x, NA), 
         sp = ifelse(!is.na(party_id),
                     ifelse(party_id == 1 | party_id == 7,1,0),NA), 
         demrep = ifelse(party_id < 4,1,
                         ifelse(party_id > 4,0,NA)), 
         ideo_fold = ifelse(V241177 > 0, abs(V241177 - 4),NA),
         baplus = ifelse(V241465x > 0,
                         ifelse(V241465x > 3,1,0),NA),
         age = ifelse(V241458x > 0, V241458x, NA), 
         male = ifelse(V241550 > 0, 
                       ifelse(V241550 == 1,1,0),NA), 
         white = ifelse(V241501x > 0,
                        ifelse(V241501x == 1,1,0),NA),
         ## political interest -- higher values mean *more* interest 
         pol_interest = ifelse(V241004 > 0, -1*V241004 + 6, NA),
         inparty_ft = ifelse(party_id < 4, dem_ft, 
                             ifelse(party_id > 4, rep_ft, NA)),
         outparty_ft = ifelse(party_id < 4, rep_ft, 
                             ifelse(party_id > 4, dem_ft, NA)),  
         ft_diff = inparty_ft - outparty_ft,
         ## out-party responses 
         extreme_negative = ifelse(outparty_ft == 0,1,0),
         extreme_positive = ifelse(inparty_ft == 100,1,0),
         gt25 = ifelse(outparty_ft > 24,1,0),
         ## mode of interview: just web vs. FTF 
         web = ifelse(V240002a < 3, 
                      ifelse(V240002a == 2,1,0),NA)) 


## Graph of in-party/out-party animosity in both samples 

p1<- nes %>% 
  filter(!is.na(web)) %>% 
ggplot(data = .) + 
  geom_density(aes(x=outparty_ft,
                   group=web, 
                   weight = V240107a,
                   linetype = as.factor(web),
                   fill = as.factor(web)), alpha = 0.65) + 
  theme_bw() + 
  ylab("") +
  xlab("Out-Party Feeling Thermometer") + 
  labs(subtitle="A",
       caption="ANES 2024, Web Only (N=5,200)",
       y="Density") + 
  scale_fill_grey(name="", 
                  labels=c("FTF","Online"), 
                  start = 0.10, 
                  end = 0.8) + 
  scale_linetype(name="",
                 labels=c("FTF","Online"),
                 limits=c(0,2)) +
  theme(legend.position = "bottom")
#theme(plot.title = element_text(hjust=0.5),
   #     legend.position = "bottom",
    #    axis.text.y = element_blank(),
     #   axis.ticks.y = element_blank(),
      #  panel.grid = element_blank()) 
p1
## In-Party Ratings 

p2 <- nes %>% 
  filter(!is.na(web)) %>% 
  ggplot(data = .) + 
  geom_density(aes(x=inparty_ft,
                   group=web, 
                   weight = V240107a,
                   linetype = as.factor(web),
                   fill = as.factor(web)), alpha = 0.65) + 
  theme_bw() + 
  ylab("") +
  xlab("In-Party Feeling Thermometer") + 
  labs(subtitle="B",
       caption="ANES 2024, Web Only (N=5,200)",
       y="Density") + 
  scale_fill_grey(name="", 
                  labels=c("FTF","Online"), 
                  start = 0.10, 
                  end = 0.8) + 
  scale_linetype(name="",
                 labels=c("FTF","Online"),
                 limits=c(0,2)) + 
  theme(legend.position = "bottom")
#  theme(plot.title = element_text(hjust=0.5),
 #       legend.position = "bottom",
  #      axis.text.y = element_blank(),
   #     axis.ticks.y = element_blank(),
    #    panel.grid = element_blank()) 
p2

png(file = "dot_code_figs/figs/figure-3.png",
    height = 8.5,
    width = 11,
    units = "in",       # Use inches to match pdf dimensions
    res = 300)          # Set resolution (DPI) for high-quality output

print(ggarrange(p1,p2,
                nrow = 1, ncol = 2, common.legend = T, legend = "bottom"))  

dev.off()


